package com.example.a2207_zh.register

import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.SpannableStringBuilder
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.text.style.ForegroundColorSpan
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.a2207_zh.R
import com.example.a2207_zh.databinding.ActivityRegisterBinding
import com.example.a2207_zh.mvvm.BaseActivity

@Route(path = "/app/RegisterActivity")
class RegisterActivity : BaseActivity<ActivityRegisterBinding,RegisterViewModel>() {
    override fun getLayout(): Int  = R.layout.activity_register

    override fun getViewModel(): Class<RegisterViewModel>  = RegisterViewModel::class.java

    override fun initView() {
        binding.btnRegister.setOnClickListener {
            val username = binding.etName.text.toString()
            val password = binding.etPwd.text.toString()
            val repassword = binding.repwd.text.toString()
            if(binding.cb.isChecked){
                viewModel.register(username, password, repassword)
            }else{
                ToastUtils.showLong("您未勾选下方协议同意框")
            }
        }

        //富文本复选框

        val builder = SpannableStringBuilder(binding.cb.text.toString())
        builder.setSpan(ForegroundColorSpan(Color.RED),3,4,SpannableStringBuilder.SPAN_INCLUSIVE_INCLUSIVE)
        val clickableSpan = object:ClickableSpan(){
            override fun onClick(widget: View) {
                //跳转到具体详情界面---webview查看
                ARouter.getInstance().build("/app/WebViewActivity")
                    .withString("url","http://sina.com")
                    .navigation()
            }
        }
        builder.setSpan(clickableSpan,6,12,SpannableStringBuilder.SPAN_INCLUSIVE_INCLUSIVE)
        binding.cb.movementMethod = LinkMovementMethod.getInstance()///移除原来的点击事件不然富文本的点击事件不起作用

        binding.cb.text = builder
    }

    override fun initData() {
        viewModel.success.observe(this){
            ToastUtils.showLong(it.message)
            if(it.code==200){
                finish()
                //跳转到登录界面且传值
                ARouter.getInstance().build("/app/LoginActivity")
                    .withString("username",it.data.username)
                    .withString("password",it.data.password)
                    .navigation()
            }
        }
    }

}